#ifndef ANDQUERY_H
#define ANDQUERY_H
#include "BinaryQuery.h"

class AndQuery: public BinaryQuery{
    friend Query operator& (const Query&,const Query&);
    AndQuery(const Query &left,const Query &right):BinaryQuery(left,right,"&"){}
    //具体的类:AndQuery继承了rep并且定义了其他纯虚函数
    QueryResult eval(const TextQuery&) const ;
};

inline Query operator& (const Query &lhs,const Query &rhs){
    // if(DEBUG) cout << "AndQuery(" << lhs.rep() << "," << rhs.rep() << ") constructed" << endl;
    return shared_ptr<Query_base>(new AndQuery(lhs,rhs));
}

QueryResult AndQuery::eval(const TextQuery& text) const{
    QueryResult left = lhs.eval(text),right = rhs.eval(text);
    auto ret_idxs = make_shared<std::set<int>>();
    //将left和right集合的交集用迭代器写入ret集合中
    std::set_intersection(left.begin(),left.end(),right.begin(),right.end(),
    std::inserter(*ret_idxs,ret_idxs->begin()));
    return QueryResult(rep(),ret_idxs,left.get_file());
}

#endif 